package com.kkb.kkbadmin.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.kkbadmin.domain.Recommend;
import com.kkb.kkbadmin.service.RecommendService;
import com.kkb.kkbcommon.api.CommonPage;
import com.kkb.kkbcommon.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: liuqihong
 * @Description:
 * @Date Created in  2021-08-18 15:24
 * @Modified By:
 */
@RestController
@Api(tags = "内推信息管理", value = "")
@RequestMapping("/recommend")
public class RecommendController {

    @Autowired
    private RecommendService recommendService;

    /**
     * 分页查询
     *
     * @param condition 条件
     * @param pageNum   页码
     * @param pageSize  每页显示行数
     * @return
     */
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "condition", value = "内推筛选条件", dataType = "Recommend"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", defaultValue = "10", dataType = "int")
    })
    @GetMapping("/page")
    public CommonResult<CommonPage<Recommend>> listPage(Recommend condition,
                                                        @RequestParam(defaultValue = "1") Integer pageNum,
                                                        @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Recommend> page = recommendService.findPage(condition, pageNum, pageSize);
        CommonPage<Recommend> commonPage = CommonPage.restPage(page.getList());
        return CommonResult.success(commonPage);
    }

    /**
     * 条件查询
     *
     * @param recommendByCondition
     * @return
     */
    @ApiOperation("指定条件查询")
    @PostMapping("/conditionQuery")
    public CommonResult<List<?>> conditionQuery(@RequestBody Recommend recommendByCondition) {
        List<Recommend> recommendList = recommendService.selectByCondition(recommendByCondition);
        return CommonResult.success(recommendList);
    }

    /**
     * 添加内推信息 公司名验证，没有则添加
     *
     * @Author leo
     * @Date: 2021/8/11 4:46 下午
     */
    @ApiOperation("添加指定内推信息")
    @PostMapping("/addRecommend")
    public CommonResult<?> addRecommend(@Validated @RequestBody Recommend recommend) {
        recommendService.addRecommend(recommend);
        return CommonResult.success(recommend);
    }

    /**
     * @Description: 根据id修改内推信息 基本功能实现
     * @Author leo
     * @Date: 2021/8/11 4:48 下午
     */
    @ApiOperation("根据id修改内推信息")
    @ApiImplicitParam(name = "id", value = "内推id", required = true, dataType = "Long")
    @PutMapping("/{id}")
    public CommonResult<?> updateRecommendById(@Validated @RequestBody Recommend recommend,
                                               @PathVariable Long id) {
        recommendService.updateByRecommendId(id, recommend);
        return CommonResult.success(null);
    }

    /**
     * 根据id逻辑删除，删除成功返回删除的id，删除失败返回失败状态码
     *
     * @param id
     * @return com.kkb.kkbcommon.api.CommonResult<?>
     * @author leo
     * @date 2021/8/15 1:49 上午
     */
    @ApiOperation("根据id删除内推信息")
    @ApiImplicitParam(name = "id", value = "内推id", required = true, dataType = "Long")
    @DeleteMapping("/{id}")
    public CommonResult<?> deleteRecommendById(@PathVariable("id") Long id) {
        recommendService.deleteByRecommendId(id);
        return CommonResult.success(null);
    }

}
